package com.hs.auth.action;
 
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.model.HsRole;
import com.hs.auth.service.IRoleService;
import com.hs.core.action.BaseAction;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
@Controller  
public class RoleController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(RoleController.class);
	private IRoleService roleService;
	@Autowired  
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	 /**
	  * 角色信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/roleInfo.do", method = RequestMethod.GET)   
     public ModelAndView roleInfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String roleid = request.getParameter("roleid");//是否存在此参数表明是新增还是修改
         if(StringUtils.isNotEmpty(roleid)){
        	 HsRole role = (HsRole)roleService.findByID(HsRole.class, new Long(roleid));
        	 modelMap.put("obj", role);
         }
		 return new ModelAndView("",modelMap);
		 
	 }
	 /**
	  * 角色信息保存
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/saveRole.do", method = RequestMethod.POST)   
     public ModelAndView saveRole(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String roleid = request.getParameter("roleid");//是否存在此参数表明是新增还是修改
         String rolename = request.getParameter("rolename");
         String rolecode = request.getParameter("rolecode");
         String roletype = request.getParameter("roletype");
         String rolememo = request.getParameter("rolememo"); 
         HsRole role = null;
         if(StringUtils.isNotEmpty(roleid)){
        	 role = (HsRole)roleService.findByID(HsRole.class, new Long(roleid));
         }else{
        	 role = new HsRole();
         }
        
         role.setRolecode(rolecode);
         role.setRolememo(rolememo);
         role.setRoletype(new Long(roletype));
         role.setRolename(rolename); 
         roleService.saveObject(role);
         
         modelMap.put("obj", role);
		 return new ModelAndView("",modelMap);
		 
	 }
	 /**
	  * 角色列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/auth/listRole.do", method = RequestMethod.GET)   
     public ModelAndView listRole(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    PageParam pageParam = new  PageParam(request,"data/listData.do");
			StringBuffer buffer = new StringBuffer();
			 
//		    pageParam.getConditionMap().put("orderName", orderName);
//		    pageParam.getConditionMap().put("orderNum", orderNum);
		    
		    //排序结束
			PageUtil pageUtil = null;
			StringBuffer searchBuffer = new StringBuffer(" select a from HsRole a where a.isdelete='0'  ");
			String prename = request.getParameter("prename"); 
			String precomfrom = request.getParameter("precomfrom"); 
			String pstatus = request.getParameter("pstatus"); 
			String dbsourse = request.getParameter("dbsourse"); 

			 
			if(StringUtils.isNotBlank(prename)){
				searchBuffer.append(" and a.prename like ?");
				pageParam.getConditionMap().put("prename", prename);
				request.setAttribute("prename", prename);
			}  
			 
			 searchBuffer.append(" order by a.prename,a.precomfrom");
			 
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = roleService.getRoleList(pageParam);
			modelMap.put("dataList", pageUtil);   
			//List dbsList = dataService.findList("select a  from PdmsDsConfig a ");
	 		//data.put("dbslist", dbsList);
			return new ModelAndView("../../app/data/dataList",modelMap);   
		 
	 }
	 
}
